package puzzle.projecteuler.p100;

import java.util.Arrays;

import astudy.util.AdvMath;

/**
 * question: Why only 11 numbers? 
 * @author wong tong
 *
 */
public class Problem037 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Integer[] tmp = AdvMath.primes(1000000);
		String[] ps = new String[tmp.length];
		for (int i = 0; i < ps.length; i ++) {
			ps[i] = tmp[i].toString();
		}
		Arrays.sort(ps);
		int sum = 0;
		for (String p: ps) {
			if (check(p, ps)) {
				sum += Integer.valueOf(p);
			}
		}
		System.out.println(sum);
	}

	private static boolean check(String p, String[] ps) {

		if (p.length() == 1) {
			return false;
		}
		for (int i = 1; i < p.length(); i ++) {
			if (Arrays.binarySearch(ps, p.substring(i)) < 0) {
				return false;
			}
			if (Arrays.binarySearch(ps, p.substring(0, i)) < 0) {
				return false;
			}
		}
		return true;
	}

}
